package by.bucha.testproject.adapters;

import by.bucha.testproject.R;
import by.bucha.testproject.content.contract.TaskContract;

import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class PeopleListAdapter extends CursorAdapter {

	public PeopleListAdapter(Context context, Cursor c) {
		super(context, c, true);
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		ViewHolder viewHolder = (ViewHolder) view.getTag();
		viewHolder.setModelId(cursor.getInt(cursor
				.getColumnIndex(TaskContract.WorkerColumns.WORKER_ID)));

		viewHolder.getNameTextView().setText(
				cursor.getString(cursor
						.getColumnIndex(TaskContract.WorkerColumns.NAME)));
		String workerType = cursor.getString(cursor
				.getColumnIndex(TaskContract.WorkerColumns.WORKER_TYPE));
		String positinText = context.getResources().getString(
				R.string.personal_list_fragment_strings_position);

		if (workerType
				.equals(TaskContract.WorkerColumns.SIMPLE_WORKER_TABLE_TYPE)) {
			positinText += context.getResources().getString(
					R.string.personal_list_fragment_strings_simple_worker);
		} else if (workerType
				.equals(TaskContract.WorkerColumns.LEADER_TABLE_TYPE)) {
			positinText += context.getResources().getString(
					R.string.personal_list_fragment_strings_leader);
		}
		viewHolder.getPositionTextView().setText(positinText);

		view.setTag(viewHolder);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		View view = View.inflate(context, R.layout.view_fragment_personal_list,
				null);
		ViewHolder viewHolder = new ViewHolder();

		viewHolder.setNameTextView((TextView) view
				.findViewById(R.id.view_fragment_personal_list_text));

		viewHolder.setPositionTextView((TextView) view
				.findViewById(R.id.view_fragment_personal_list_posotion));

		view.setTag(viewHolder);
		return view;
	}

	public class ViewHolder {

		private int mModelId;

		private TextView mNameTextView;

		private TextView mPositionTextView;

		public void setPositionTextView(TextView positionTextView) {
			this.mPositionTextView = positionTextView;
		}

		public TextView getPositionTextView() {
			return mPositionTextView;
		}

		public void setNameTextView(TextView nameTextView) {
			this.mNameTextView = nameTextView;
		}

		public TextView getNameTextView() {
			return mNameTextView;
		}

		public void setModelId(int modelId) {
			mModelId = modelId;
		}

		public int getModelId() {
			return mModelId;
		}
	}
}
